<?php
namespace Client\Controller;

use Think\Image;

use Think\Controller;
class UploadController extends Controller {
	public function upload(){
		if(IS_POST){
			$files = $_FILES;
			$data = $_POST;
			file_put_contents("Public/Log/log.txt", "image:".json_encode($_FILES)."\r\n",FILE_APPEND|LOCK_EX);
			file_put_contents("Public/Log/log.txt", "image:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$data["code"] = 0;
			foreach ($files as $file){
				$uploadpath = "Public/Upload/Yue/";//上传路径

				$util = A('Util');
				$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

				$pinfo = pathinfo($file['name']);
				$ftype = $pinfo['extension'];//文件类型
				$name = basename($file['name'],".{$ftype}");

				$fileName = md5($name.time())."_{$data["code"]}.".$ftype;//识别图
				$image = new Image();
				$image->open($file['tmp_name']);
				$image->save($uploadpath.$fileName);
//				file_put_contents("Public/Log/log.txt", "image:".$file['name']."\r\n",FILE_APPEND|LOCK_EX);

				$data["image"][] = $uploadpath.$fileName;
				$data["code"]++;
			}
			$this->ajaxReturn($data);
		}else{
			$this->display();
		}
	}
	public function upload2(){
		if(IS_POST){
			$files = $_FILES['image'];
			$count = count($files['name']);
			for($i = 0;$i <$count;$i++){
				$uploadpath = "Public/Upload/Yue/";//上传路径

				$util = A('Util');
				$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

				$pinfo = pathinfo($files['name'][$i]);
				$ftype = $pinfo['extension'];//文件类型
				$name = basename($files['name'][$i],".{$ftype}");

				$fileName = md5($name.time()).".".$ftype;//识别图
				$image = new Image();
				$image->open($files['tmp_name'][$i]);
				$image->save($uploadpath.$fileName);
				//				file_put_contents("Public/Log/log.txt", "image:".$uploadpath.$fileName."\r\n",FILE_APPEND|LOCK_EX);

				$data["image"][] = $uploadpath.$fileName;
				$data['err'] = 1;
			}
			$this->ajaxReturn($data);
		}else{
			$this->display();
		}
	}
}